package cc.servlet;

import cc.entity.Book;
import cc.entity.Page;
import cc.util.DateUtil;
import cc.service.BookService;
import cc.service.impl.BookServiceImpl;
import cc.util.FileImageUtil;
import cc.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BookServlet
 * @Description:
 * @Author: 晓东√
 * @Date: 2024-09-04 09:39
 * @Version: 1.0
 **/
@WebServlet("/book.action")
@MultipartConfig
public class BookServlet extends HttpServlet {

    BookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String opr = req.getParameter("opr") == null ? "list" : req.getParameter("opr");
        switch (opr) {
            case "list":
                //展示书籍列表
                //获取前端参数
                list(req, resp);
                return;
            case "edit":
                edit(req, resp);
                return;
            case "detail":
                detail(req, resp);
                return;
        }

    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        Book book = bookService.detail(id);

        String json = JSON.toJSONString(book, SerializerFeature.WriteNullStringAsEmpty);

        resp.getWriter().print(json);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Book book = new Book();
        String bookId = req.getParameter("bookId");
        book.setBookName(req.getParameter("bookName"));
        book.setAuthor(req.getParameter("author"));
        book.setPublisher(req.getParameter("publisher"));
        Date publishDate = DateUtil.dateFormat(req.getParameter("publishDate"));
        book.setPublishDate(publishDate);
        book.setStockQuantity(Integer.parseInt(req.getParameter("stockQuantity")));
        book.setDescription(req.getParameter("description"));

        //书籍封面
        Part part = req.getPart("coverImage");
        //上传到服务器的路径
        String uploadUrl = FileImageUtil.fileUploadUrl(req, "/upload", part);
        book.setCoverImage(uploadUrl);
        book.setBookType(1);
        boolean b;
        if (bookId == null||"".equals(bookId)) {

            book.setIsbn(UUIDUtil.uuid());
            b = bookService.saveBook(book);
        } else {
            book.setBookId(Integer.parseInt(bookId));
            b = bookService.updateBook(book);
        }
        Map<String, String> map = new HashMap<>();
        if (b) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }

        String jsonString = JSON.toJSONString(map);
        resp.getWriter().print(jsonString);
    }


    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String bookName = req.getParameter("bookName") == "" ? null : req.getParameter("bookName");
        String author = req.getParameter("author") == "" ? null : req.getParameter("author");

       int pageNum= req.getParameter("pageNum")==""?1:Integer.parseInt(req.getParameter("pageNum"));

        Page<Book> books = bookService.selectAll(bookName, author,pageNum);

        String json = JSON.toJSONString(books, SerializerFeature.WriteNullStringAsEmpty);
        resp.getWriter().print(json);
    }
}
